package com.situ.company.employee.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class EmployeeDao {

    public Integer insert(EmployeeModel model) {
        String sql = "insert into Employee (code,name,pass,code_dept) values(?,?,?,?)";
        return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getPass(), model.getCodeDept());
    }
    
    public Integer delete(EmployeeModel model) {
        String sql = "delete from employee where code = ?";
        return JDBCUtil.update(sql, model.getCode());
    }
    
    public Integer updateName(EmployeeModel model) {
        String sql = "update employee set name = ? where code=?";
        return JDBCUtil.update(sql, model.getName(), model.getCode());
    }
    
    public Integer updatepass(EmployeeModel model) {
        String sql = "update employee set name = ? where code=?";
        return JDBCUtil.update(sql, model.getPass(), model.getCode());
    }
    
    public Integer updateCodeDept(EmployeeModel model) {
        String sql = "update employee set code_dept = ? where code=?";
        return JDBCUtil.update(sql, model.getCodeDept(), model.getCode());
    }
    
    public Integer updateActive(EmployeeModel model) {
        StringBuffer sql = new StringBuffer("update employee set id=id ");
        List<Object> vals = new ArrayList<>();
        String name = model.getName();
        if(name != null) {
            sql.append(" ,name = ? ");
            vals.add(name);
        }
        String pass = model.getPass();
        if(pass != null) {
            sql.append(" ,pass = ? ");
            vals.add(pass);
        }
        String codeDept = model.getCodeDept();
        if(codeDept != null) {
            sql.append(" ,code_dept = ? ");
            vals.add(codeDept);
        }
        String image = model.getImage();
        if(image != null) {
            sql.append(" ,image = ? ");
            vals.add(image);
        }
        sql.append(" where code = ? ");
        vals.add(model.getCode());
        return JDBCUtil.update(sql.toString(), vals);
    }
    
    public List<EmployeeModel> selectList(EmployeeModel model) {
        StringBuffer sql = new StringBuffer("select code,name, code_dept,(select name from department where code_dept = code) name_dept, image from employee ");
        List<Object> vals = appendWhere(sql, model);
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("codeDept", "code_dept");
        fields.put("nameDept", "name_dept");
        fields.put("image", "image");
        return JDBCUtil.queryList(sql.toString(), vals, EmployeeModel.class, fields);
    }
    
    public EmployeeModel selectModel(EmployeeModel model) {
        StringBuffer sql = new StringBuffer("select code,name,pass,code_dept,image from employee");
        List<Object> vals = appendWhere(sql, model);
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("pass", "pass");
        fields.put("codeDept", "code_dept");
        fields.put("image", "image");
        return JDBCUtil.queryModel(sql.toString(), vals, EmployeeModel.class, fields);
    }

    private List<Object> appendWhere(StringBuffer sql, EmployeeModel model) {
        List<Object> vals = new ArrayList<>();
        sql.append(" where 1=1 ");
        String code = model.getCode();
        if (!FmtEmpty.isEmpty(code)) {
            sql.append(" and code like ? ");
            vals.add(code);
        }
        String name = model.getName();
        if (!FmtEmpty.isEmpty(name)) {
            sql.append(" and name like ? ");
            vals.add(name);
        }
        String codeDept = model.getCodeDept();
        if (!FmtEmpty.isEmpty(codeDept)) {
            sql.append(" and code_dept like ? ");
            vals.add(codeDept);
        }
        String image = model.getImage();
        if (!FmtEmpty.isEmpty(image)) {
            sql.append(" and image like ? ");
            vals.add(image);
        }
        if(model.isPageOn()) {
            sql.append(" limit ?,? ");
            vals.add(model.getRowStart());
            vals.add(model.getPageLimit());
        }
        return vals;
    }

    public Integer selectCount(EmployeeModel m0) {
        StringBuffer sql = new StringBuffer("select count(1) from employee ");
        List<Object> vals = appendWhere(sql, m0);
        return JDBCUtil.queryInt(sql.toString(), vals);
    }

}
